﻿using Confluent.Kafka.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace log4net.extension.kafka
{
    public class LogEntitySerializer : ISerializer<Log4netLogEntity>
    {
        public byte[] Serialize(Log4netLogEntity data)
        {
            if (data == null)
                return null;

            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, data);
                ms.Position = 0;
                byte[] bytes = new byte[ms.Length];
                ms.Read(bytes, 0, bytes.Length);
                return bytes;
            }
        }
    }
}
